#include "drv_led.h"
#include "drv_beep.h"
#include "drv_systick.h"
#include "drv_key.h"
#include "drv_exti.h"
#include "drv_fan.h"
#include "drv_usart.h"
#include "drv_motor.h"
#include "drv_timer.h"
#include <stdio.h>
#include "FreeRTOS.h"
#include <task.h>
#include <queue.h>
#include "drv_wifi.h"
#include "drv_sht20.h"
#include "drv_74hc595.h"
#include "drv_servo.h"

uint8_t led_flg=0;
uint8_t key_flg1=0;
uint8_t key_flg2=0;
uint8_t key_flg3=0;
uint8_t key_flg4=0;
volatile uint8_t humiture_flg5=1;
volatile uint8_t humiture_flg6=0;
uint8_t wifi_led_flg=0;
uint8_t v_beep_flg=0;//蜂鸣器
uint8_t v_fan_flg=0;//风扇
uint8_t v_motor_flg=0;//马达
uint8_t stand_flg=0;//站立
uint8_t down_flg=0;//趴下
uint8_t forward_flg=0;//前进
uint8_t back_flg=0;//后退
uint8_t sit_flg=0;//坐下
uint8_t stop_flg=0;//停下



int main(void)
{
	uint8_t angle = 0;
	sht20_val data = {0};
	static uint8_t humiture_flg7=1;
	uint16_t val;
	uint8_t i,a,b,c,d;
	char str[] = "High temperature";
	//BaseType_t ret;
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	drv_led_init();
	drv_beep_init();
	drv_fan_init();
	drv_motor_init();
	drv_systick_init(168);
	drv_sht20_init();
	drv_74hc595_init();
	drv_timer3_init();
	drv_timer4_init();
	drv_key_init();
	drv_extif9_init();
	drv_extic_init();
	drv_usart3_init();
	drv_usart6_init();
	drv_usart2_init();
	drv_pwm_init();
	//drv_wifi_init();
	//drv_timer6_init();

	Servo_Init();
	while(1)
	{
		if(wifi_led_flg)drv_led_open();else{drv_led_close();}
		if(v_beep_flg)drv_beep_on();else{drv_beep_off();}
		if(v_fan_flg)drv_fan_on();else{drv_fan_off();}
		if(v_motor_flg)drv_motor_on();else{drv_motor_off();}
		if(stand_flg){drv_systick_ms(200);Servo_Init();stand_flg=0;}
		if(down_flg){Get_down();drv_systick_ms(200);down_flg=0;}
		if(forward_flg){drv_systick_ms(200);Move_Forward();}
		if(back_flg){drv_systick_ms(200);Move_Backward();}
		if(sit_flg){drv_systick_ms(200);Sit_down();sit_flg=0;}
		if(humiture_flg6 == 0)
		{
				
			if(humiture_flg7 != humiture_flg6)
			{
				data = drv_sht20_getdata();
				if(data.hum_T>=25.00)
				{
					usart_sendstr(USART2,str,strlen(str));
					//usart_sendstr(USART2,str,16);
				}
			}
			if(humiture_flg5==0)
			{
				val = (uint16_t)(data.hum_T*100);
			}
			else if(humiture_flg5==1)
			{
				val = (uint16_t)(data.hum_RH*100);
			}
			a =  val / 1000;      // 千位（十位数）
			b = (val / 100) % 10;  // 百位（个位数）
			c = (val / 10) % 10;   // 十位（小数点后第一位）
			d = val % 10;        // 个位（小数点后第二位）
			humiture_flg7 = humiture_flg6;
		}
		else
		{
			humiture_flg7 = humiture_flg6;
		}
		drv_seg_display(1, 10);
		drv_seg_display(0, a);
		drv_seg_display(1, b);
		drv_seg_display(2, c);
		drv_seg_display(3, d);
	}
}

